package com.tuling.code.io.pseudoasync;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 执行线程池
 * @author javaw
 *
 */
public class TimeServerExecPoll {
	private ExecutorService executor;
	
	private int corePoolSize = Runtime.getRuntime().availableProcessors();
	private static long keepAliveTime = 120L;
	private static TimeUnit UNIT = TimeUnit.SECONDS;
	
	public TimeServerExecPoll(int maxPollSize , int queueSize ){
		executor = new ThreadPoolExecutor(corePoolSize, maxPollSize, keepAliveTime, UNIT, new ArrayBlockingQueue<java.lang.Runnable>(queueSize), new MyRejectPolicy());
	}
	
	public void execute(Runnable task){
		executor.execute(task);
	}
	
	public class MyRejectPolicy implements RejectedExecutionHandler{  
	    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {  
	        //Sender是我的Runnable类，里面有message字段  
	        if (r instanceof TimeServerHandler) {  
	        	TimeServerHandler sender = (TimeServerHandler) r;  
	            //直接打印  
	            System.out.println(sender.toString());  
	        }  
	    }  
	}  }
